#include "ULeafWindow.h"
#include "USpaceFillingContainerWindow.h"
#include "UNonSpaceFillingContainerWindow.h"
#include "USplittedContainerWindow.h"
#include "UContainerWindow.h"
#include "UCustomWindow.h"

int LastWindowTypeBuildings= 999;
int LastWindowTypeUnits    = 1999;

using namespace std;

TLeafWindow::TLeafWindow(TRectangle Rect, TCustomWindow* child, TContainerWindow* parent, TWindowThemer *wt, std::string name, int tag)
: TDockable(Rect, parent, wt, name)
{
	Tag = tag;
	Child=child;

}

bool TLeafWindow::Dock(TDockable* WindowToDock, int X, int Y)
{
	int QuarterWindowWidth = (Right -Left) / 4;
	int QuarterWindowHeight= (Bottom-Top ) / 4;

	if	((X>Left+QuarterWindowWidth ) &&  (X<Right -QuarterWindowWidth)&&
		 (Y>Top +QuarterWindowHeight) &&  (Y<Bottom-QuarterWindowHeight))
	{
		TRectangle Rect(Left, Top, Right, Bottom);
		TContainerWindow* NewParent;

		if (X>Left+Width()/2)
			NewParent = new TSplittedContainerWindow(	 Rect, Parent, windowthemer, this->Name+", "+WindowToDock->Name);
		else
			NewParent = new TSpaceFillingContainerWindow(Rect, Parent, windowthemer, this->Name+", "+WindowToDock->Name);


		TNonSpaceFillingContainerWindow* Root =  GetRoot();

		Parent->RemoveChild(this);
		WindowToDock->Parent->RemoveChild(WindowToDock);


		Parent->AddChild(NewParent);

		NewParent->AddChild(this);
		NewParent->AddChild(WindowToDock);

		NewParent->SetPos(Rect);
		NewParent->setActiveChild(1);
		return true;
	}
	else
	{
		return false;
	}

}


void TLeafWindow::Draw() {
	//TODO: This could(and should) be made into a global 'saveGLstate' macro
	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
	glPushMatrix();
	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glPushMatrix();

	glColor3f(0.35, 0.3, 0.3);
	glBegin(GL_QUADS);
	glVertex2i(Left		, Top);
	glVertex2i(Right	, Top);
	glVertex2i(Right	, Bottom);
	glVertex2i(Left		, Bottom);
	glEnd();


	TDockable::Draw();

	int width = GetRoot()->Width();
	int height = GetRoot()->Height();

	if (Child->Is3d)
	{	//////////////Set up opengl for drawing 3d/////////////////
		glViewport(Left,height-Top-Height(),Width(), Height());
		glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
		glLoadIdentity();									// Reset The Projection Matrix
		gluPerspective(45.0f,(GLfloat)Width()/(GLfloat)Height(),1.0f,1000.0f);
		glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
		glLoadIdentity();

		glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
		glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
		glDepthFunc(GL_LESS);								// The Type Of Depth Testing To Do
		glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
		//////////////Set up opengl for drawing 3d/////////////////
		glClear(GL_DEPTH_BUFFER_BIT); //Clear window for overlapping
	}

	if(!Child->Is3d) glTranslatef(Left, Top, 0);
 	glScissor(Left, height-Bottom, Right-Left, Bottom-Top);

	glEnable(GL_SCISSOR_TEST);
	Child->Draw();
	glDisable(GL_SCISSOR_TEST);

	if (Child->Is3d)
	{
		glViewport(0,0, ScreenWidth, ScreenHeight);
		//////////////Set up opengl for drawing 2d/////////////////
		glDisable(GL_DEPTH_TEST);
		glShadeModel(GL_SMOOTH);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glOrtho(0, ScreenWidth, ScreenHeight, 0, -1, 1);
		glMatrixMode(GL_MODELVIEW);
		//////////////Set up opengl for drawing 2d/////////////////
	}

	//TODO: This could(and should) be made into a global 'restoreGLstate' macro
	glColor3f(1,1,1);
	glClearColor(0.8f, 0.8f, 0.8f, 0.0f); //FIXME: This is the default clearcolor, and is now in 2 places
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	glDisable(GL_CULL_FACE);
	glDisable(GL_LIGHTING);
	glDisable(GL_LIGHT0);
	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
	glPopMatrix();
}

void TLeafWindow::MouseMove	(int X, int Y, int mstate)
{
	TDockable::MouseMove(X, Y, mstate);
	Child->MouseMove(X-Left, Y-Top, mstate);
}

void TLeafWindow::MouseLeave()
{
	Child->MouseLeave();
}

void TLeafWindow::MouseDown	(int X, int Y, uint8 button)
{
	TDockable::MouseDown(X, Y, button);
	Child->MouseDown(X-Left, Y-Top, button);
}

void TLeafWindow::MouseUp	(int X, int Y, uint8 button)
{
	TDockable::MouseUp(X, Y, button);
	Child->MouseUp(X-Left, Y-Top, button);
}

void TLeafWindow::KeyDown(SDL_KeyboardEvent* Key)
{
	TDockable::KeyDown(Key);
	if (Key->keysym.mod & KMOD_CTRL)
	{
		if (Key->keysym.sym == SDLK_d)
			GetRoot()->AddChild(new TLeafWindow(TRectangle(Left+20, Top+20, Right+20, Bottom+20), Child, Parent, windowthemer, "Copy of: "+Name));
	}

	Child->KeyDown(Key);
}

void TLeafWindow::KeyUp(SDL_KeyboardEvent* Key)
{
	TDockable::KeyUp(Key);
	Child->KeyUp(Key);
}

void TLeafWindow::SetPos(TRectangle Rect)
{
	TDockable::SetPos(Rect);
	Child->resize(Rect.Right-Rect.Left, Rect.Bottom-Rect.Top);
}

TDockable* TLeafWindow::FindWindowByTag(int tag)
{
	if (Tag == tag)
		return this;
	else
		return NULL;
}
